<?php
namespace app\libs;
/**
 * 百度 sitemap 生成类
 */
class Rssbaidu {
    /**
     * RSS 单条信息的数组
     */
    private $items = [];

    /**
     * 添加 RSS 项
     * @param string $title  标题
     * @param string $link  链接
     * @param string $pubDate  文章日期
     */
     function addItem($title, $link, $pubDate) {
        $this->items[] = [
            'title' => $title,
            'link' => $link,
            'pubDate' => $pubDate
        ];
     }

    /**
     * 生成 RSS
     */
    public function fetch() {
        $rss = '<?xml version="1.0" encoding="utf-8" ?>';
        $rss .= '<urlset>';

        for ($i = 0, $len=count($this->items); $i < $len; $i++) {
            $rss .= '<url>';
            $rss .= "<loc>{$this->items[$i]['link']}</loc>";
            $rss .= "<lastmod>{$this->items[$i]['pubDate']}</lastmod>";
            $rss .= '<priority>0.5</priority>';
            $rss .= '</url>';
        }

        $rss .= '</urlset>';

        return $rss;
    }

    /**
     * 输出
     */
    public function show() {
        header('Content-Type: text/xml; charset=utf-8');
        echo $this->fetch();
        exit;
    }

}
